﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using RN.ERP.Web.ModelService;
using Telerik.Web.UI;

namespace RN.ERP.Web.EnterpriseChange
{
    public partial class ChangeList : BasePage
    {
        BasicService.BasicServiceClient basicClient = new BasicService.BasicServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ListCorps_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            int corpChangeCount=0; 
            string flowCode = ddlStatus.SelectedValue;
            var strCorpChangeList = basicClient.GetChanges(out corpChangeCount, "corp", "", "", GetCondition(), ListCorps.CurrentPageIndex + 1, ListCorps.PageSize, UserInfo.CurrentArea);
            var corpChangeList = JsonConvert.DeserializeObject<List<V_ChangeInfo>>(strCorpChangeList);
            ListCorps.VirtualItemCount = corpChangeCount;
            ListCorps.DataSource = corpChangeList;
        }

        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "Rebind")
            {
                ListCorps.MasterTableView.SortExpressions.Clear();
                ListCorps.MasterTableView.GroupByExpressions.Clear();
                ListCorps.Rebind();
            }

        }

        private string GetCondition()
        {
            string condition = string.Empty;
            string corpName=txtCorpName.Text.Trim();
            var flows=ddlStatus.SelectedValue;
            if(!string.IsNullOrEmpty(corpName))
            {
                condition += " and isrtusername like '%" + corpName + "%' ";
            }
            if (!string.IsNullOrEmpty(flows))
            {
                string flowCon = " and flowcode in(";
                foreach (var f in flows.Split('|'))
                {
                    flowCon += "'" + f + "',";
                }
                flowCon = flowCon.TrimEnd(',');
                flowCon += ")";
                condition += flowCon;
            }
            return condition;
        }

        public string GetFlowCodeTitle(object oflowcode)
        {
            if (oflowcode == null)
            {
                return string.Empty;
            }
            string flowcode = oflowcode.ToString().Trim();
            if (flowcode.StartsWith("1"))
            {
                return "审核通过";
            }
            else if (flowcode.StartsWith("0,019,021"))
            {
                return "主管退回";
            }
            else if (flowcode.IndexOf(",022") > 0)
            {
                return "再次上报";
            }
            else if (flowcode.StartsWith("0,019"))
            {
                return "已上报";
            }
            else if (flowcode.Trim().Equals("0"))
            {
                return "未上报";
            }
            else
            {
                return string.Empty;
            }
        }
    }
}